import token from "./token";

export async function fetchRequ(obj) {
  // { method, url, data, token };
  try {
    const resp = await fetch(
      "https://game-two-1310591936.cos.ap-nanjing.myqcloud.com" + obj.url,
      {
        method: obj.method,
        headers: {
          Authorization: obj.token
            ? obj.token
            : token[obj.method][obj.url.split("/")[2].split(".")[0]], // 获取文件名 [无后缀]
          "Content-Type": "application/json",
        },
        body: JSON.stringify(obj.data),
      }
    );
    const data = {};
    data.status = resp.status; // 状态码
    data.data = await resp.text(); // 响应体
    return data;
  } catch (err) {
    return err;
  }
}

// const resp = await fetchRequ({
//   method: "put",
//   url: "/data/users.json",
//   data: [{ userName: "testtest", pwd: "xxxxxx" }],
//   token:
//     "q-sign-algorithm=sha1&q-ak=AKID4wYv2tUu5zd0qPilY5LL6aomTv72h6F8&q-sign-time=1664186497;1689190097&q-key-time=1664186497;1689190097&q-header-list=&q-url-param-list=&q-signature=2841b0ee5397966b53e70ac33d0ac8d346e355da",
// });
// consolelog(resp);
